/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package sk.tuke.kpi.ssce.nbinterface.options;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.openide.util.Lookup;
import org.openide.util.NbPreferences;
import sk.tuke.kpi.ssce.projection.provider.ProjectionProviderFactory;

public final class SSCEOptions extends javax.swing.JPanel {

    private final SSCEOptionsPanelController controller;
    
    private static final Collection<? extends ProjectionProviderFactory> availableImplementations =
            Lookup.getDefault().lookupAll(ProjectionProviderFactory.class);
    
    private static final String DEFAULT_IMPLEMENTATION = "defaultProjectionsImplementation";
    private static final String USE_DEFAULT_IMPLEMENTATION = "useDefaultProjectionsImplementation";


    SSCEOptions(SSCEOptionsPanelController controller) {
        this.controller = controller;
        initComponents();
        // TODO listen to changes in form fields and call controller.changed()
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        availableImplementationsCombo = new javax.swing.JComboBox();
        useCheckBox = new javax.swing.JCheckBox();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(org.openide.util.NbBundle.getMessage(SSCEOptions.class, "SSCEOptions.jPanel1.border.title"))); // NOI18N

        List<String> names = new ArrayList<String>();
        for (ProjectionProviderFactory projectionProvider : availableImplementations) {
            names.add(projectionProvider.getDisplayName());
        }
        availableImplementationsCombo.setModel(new javax.swing.DefaultComboBoxModel(names.toArray(new String[names.size()])));

        org.openide.awt.Mnemonics.setLocalizedText(useCheckBox, org.openide.util.NbBundle.getMessage(SSCEOptions.class, "SSCEOptions.useCheckBox.text")); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(useCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(availableImplementationsCombo, 0, 338, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(useCheckBox)
                    .addComponent(availableImplementationsCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(161, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    void load() {
        useCheckBox.setSelected(useDefaultImplementation());
        ProjectionProviderFactory factory = getDefaultImplementation();
        if (factory != null) {
            availableImplementationsCombo.setSelectedItem(factory.getDisplayName());
        }
    }

    void store() {
        NbPreferences.forModule(SSCEOptions.class).putBoolean(USE_DEFAULT_IMPLEMENTATION, useCheckBox.isSelected());
        NbPreferences.forModule(SSCEOptions.class).put(DEFAULT_IMPLEMENTATION, (String) availableImplementationsCombo.getSelectedItem());
    }
    
    public static boolean useDefaultImplementation() {
        return NbPreferences.forModule(SSCEOptions.class).getBoolean(USE_DEFAULT_IMPLEMENTATION, false);
    }
    
    public static ProjectionProviderFactory getDefaultImplementation() {
        String defaultDefault = availableImplementations.isEmpty() 
                ? "" 
                : ((ProjectionProviderFactory) availableImplementations.toArray()[0]).getDisplayName();
        String realDefault = NbPreferences.forModule(SSCEOptions.class).get(DEFAULT_IMPLEMENTATION, defaultDefault);
        ProjectionProviderFactory defaultFactory = null;
        for (ProjectionProviderFactory factory : availableImplementations) {
            if (factory.getDisplayName().equals(realDefault)) {
                defaultFactory = factory;
            }
        }
        return defaultFactory;
    }

    boolean valid() {
        // TODO check whether form is consistent and complete
        return true;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox availableImplementationsCombo;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JCheckBox useCheckBox;
    // End of variables declaration//GEN-END:variables
}
